package de.mukis.gemini.sample.rcp.dialogs;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.e4.core.di.extensions.Preference;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.osgi.service.prefs.BackingStoreException;
public class PreferenceDialog extends TitleAreaDialog {
private Text txtURL;
private Text txtUser;
private Text txtPassword;
private ComboViewer comboDriver;
@Inject
@Preference
IEclipsePreferences preferences;
@Inject
Logger log;
/**
* Create the dialog.
*
* @param parentShell
*/
@Inject
public PreferenceDialog(@Named(IServiceConstants.ACTIVE_SHELL) Shell parentShell) {
super(parentShell);
setTitle("Set your database preferences");
}
/**
* Create contents of the dialog.
*
* @param parent
*/
@Override
protected Control createDialogArea(Composite parent) {
Composite area = (Composite) super.createDialogArea(parent);
Composite container = new Composite(area, SWT.NONE);
container.setLayout(new GridLayout(2, false));
container.setLayoutData(new GridData(GridData.FILL_BOTH));
Label lblDriver = new Label(container, SWT.NONE);
lblDriver.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblDriver.setText("Driver");
comboDriver = new ComboViewer(container, SWT.NONE);
comboDriver.setContentProvider(ArrayContentProvider.getInstance());
comboDriver.setInput(new String[] { "org.apache.derby.jdbc.EmbeddedDriver", "com.mysql.jdbc.Driver" });
Combo combo = comboDriver.getCombo();
combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblUrl = new Label(container, SWT.NONE);
lblUrl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblUrl.setText("URL");
txtURL = new Text(container, SWT.BORDER);
txtURL.setText("jdbc:derby:memory:test;create=true");
txtURL.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblUser = new Label(container, SWT.NONE);
lblUser.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblUser.setText("User");
txtUser = new Text(container, SWT.BORDER);
txtUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblPassword = new Label(container, SWT.NONE);
lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblPassword.setText("Password");
txtPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
txtPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
setMessage("Set your database preferences");
return area;
}
/**
* Create contents of the button bar.
*
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
@Override
protected void okPressed() {
IStructuredSelection selection = (IStructuredSelection) comboDriver.getSelection();
String driver = selection.isEmpty() ? "org.apache.derby.jdbc.EmbeddedDriver" : selection.getFirstElement().toString();
try {
preferences.put("jdbc_driver", driver);
preferences.put("jdbc_url", txtURL.getText());
preferences.putBoolean("jdbc_reconnect", true);
preferences.flush();
} catch (BackingStoreException e) {
e.printStackTrace();
}
super.okPressed();
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(450, 300);
}
}